package com.jinzhi.jzcultivate.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.itextpdf.text.pdf.PdfReader;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.config.Constant;
import com.jinzhi.common.config.IFastConfig;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.training.pojo.CoursesAndTeacherVo;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.ValuesUtil;
import com.jinzhi.oss.service.OssService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import com.jinzhi.wxmp.domain.MpArticleDO;
import com.jinzhi.wxmp.domain.MpConfigDO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import org.apache.http.entity.ContentType;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.jinzhi.api.util.TokenTag.TOKEN_TAG;

@Controller
@AllArgsConstructor
@RequestMapping("/couInfo")
public class CourseController extends BaseController {

    @Autowired
    private CoursesService coursesService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private ChapterItemBankService chapterItemBankService;
    @Autowired
    private TrainingResourcesService trainingResourcesService;
    @Autowired
    private TrainingResultsService trainingResultsService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private CoursesClassService coursesClassService;
    @Autowired
    private UserService userService;


    @Autowired
    IFastConfig ifastConfig;
    @Autowired
    private OssService ossService;

    @Autowired
    private DiscussService discussService;









    @Log("课程介绍")
    @GetMapping("/couInfo")
    String courseInfo(Model model, String id) {
        CoursesAndTeacherVo coursesAndTeacherVo = coursesService.selCourseDetail(Long.parseLong(id));
        model.addAttribute("cou", coursesAndTeacherVo);

        List<Map> mapList = new ArrayList<>();
        if (StringUtils.isNotBlank(id)) {
            model.addAttribute("coursesId", Long.parseLong(id));
            List<ChapterDO> chapterDOS = chapterService.findByKv("courses_id", Long.parseLong(id), "is_root", 1);
            if (chapterDOS != null && chapterDOS.size() > 0) {
                for (ChapterDO chapterDO : chapterDOS) {
                    Map map1 = new HashMap();
                    map1.put("id", chapterDO.getId());
                    List<ChapterItemBankDO> chapterItemBankDOS = chapterItemBankService.findByKv("chapter_id", chapterDO.getId(), "type", 0);
                    if (chapterItemBankDOS != null && chapterItemBankDOS.size() > 0) {
                        map1.put("isChaItem", "true");
                    } else {
                        map1.put("isChaItem", "false");
                    }
                    if (chapterDO.getName() != null) {
                        map1.put("name", chapterDO.getName());
                    } else {
                        map1.put("name", "");
                    }
                    map1.put("isFree", chapterDO.getIsFree());//是否免费

                    map1.put("level", chapterDO.getLevel());//层级

                    List<TrainingResourcesDO> trainingResourcesDOS = trainingResourcesService.findByKv("courses_id", Long.parseLong(id), "chapter_id", chapterDO.getId());//,"type",1
                    if (trainingResourcesDOS != null && trainingResourcesDOS.size() > 0) {
                        int i = 0;
                        for (TrainingResourcesDO r : trainingResourcesDOS) {
                            r.setRindex(i);
                            i++;
                        }
                    }
                    map1.put("resources", trainingResourcesDOS);
                    if (trainingResourcesDOS.size() > 0) {
                        map1.put("isHaveResources", 1);
                    } else {
                        map1.put("isHaveResources", 0);
                    }

                    List<Map> map1Children = new ArrayList<>();
                    List<ChapterDO> chapterDOS2 = chapterService.findByKv("courses_id", Long.parseLong(id), "parent_id", chapterDO.getId());
                    if (chapterDOS2 != null && chapterDOS2.size() > 0) {
                        for (ChapterDO chapterDO2 : chapterDOS2) {
                            Map map2 = new HashMap();
                            map2.put("id", chapterDO2.getId());
                            List<ChapterItemBankDO> chapterItemBankDOS2 = chapterItemBankService.findByKv("chapter_id", chapterDO2.getId(), "type", 0);
                            if (chapterItemBankDOS2 != null && chapterItemBankDOS2.size() > 0) {
                                map2.put("isChaItem", "true");
                            } else {
                                map2.put("isChaItem", "false");
                            }
                            if (chapterDO2.getName() != null) {
                                map2.put("name", chapterDO2.getName());
                            } else {
                                map2.put("name", "");
                            }
                            map2.put("isFree", chapterDO2.getIsFree());//是否免费

                            map2.put("level", chapterDO2.getLevel());//层级

                            List<TrainingResourcesDO> trainingResourcesDOS2 = trainingResourcesService.findByKv("courses_id", Long.parseLong(id), "chapter_id", chapterDO2.getId());//,"type",1
                            if (trainingResourcesDOS2 != null && trainingResourcesDOS2.size() > 0) {
                                int i = 0;
                                for (TrainingResourcesDO r : trainingResourcesDOS2) {
                                    r.setRindex(i);
                                    i++;
                                }
                            }
                            map2.put("resources", trainingResourcesDOS2);
                            if (trainingResourcesDOS2.size() > 0) {
                                map2.put("isHaveResources", 1);
                            } else {
                                map2.put("isHaveResources", 0);
                            }

                            List<Map> map2Children = new ArrayList<>();
                            List<ChapterDO> chapterDOS3 = chapterService.findByKv("courses_id", Long.parseLong(id), "parent_id", chapterDO2.getId());
                            if (chapterDOS3 != null && chapterDOS3.size() > 0) {
                                for (ChapterDO chapterDO3 : chapterDOS3) {
                                    System.out.println("id:" + chapterDO3.getId() + " -- name:" + chapterDO.getName());
                                    Map map3 = new HashMap();
                                    map3.put("id", chapterDO3.getId());
                                    List<ChapterItemBankDO> chapterItemBankDOS3 = chapterItemBankService.findByKv("chapter_id", chapterDO3.getId(), "type", 0);
                                    if (chapterItemBankDOS3 != null && chapterItemBankDOS3.size() > 0) {
                                        map3.put("isChaItem", "true");
                                    } else {
                                        map3.put("isChaItem", "false");
                                    }
                                    if (chapterDO3.getName() != null) {
                                        map3.put("name", chapterDO3.getName());
                                    } else {
                                        map3.put("name", "");
                                    }
                                    map3.put("isFree", chapterDO3.getIsFree());//是否免费

                                    map3.put("level", chapterDO3.getLevel());//层级

                                    List<TrainingResourcesDO> trainingResourcesDOS3 = trainingResourcesService.findByKv("courses_id", Long.parseLong(id), "chapter_id", chapterDO3.getId());//,"type",1
                                    if (trainingResourcesDOS3 != null && trainingResourcesDOS3.size() > 0) {
                                        int i = 0;
                                        for (TrainingResourcesDO r : trainingResourcesDOS3) {
                                            r.setRindex(i);
                                            i++;
                                        }
                                    }
                                    map3.put("resources", trainingResourcesDOS3);

                                    if (trainingResourcesDOS3.size() > 0) {
                                        map3.put("isHaveResources", 1);
                                    } else {
                                        map3.put("isHaveResources", 0);
                                    }
                                    map2Children.add(map3);
                                }
                                map2.put("isHaveChildren", 1);
                                map2.put("children", map2Children);
                            } else {
                                map2.put("isHaveChildren", 0);
                                map2.put("children", "");
                            }
                            map1Children.add(map2);
                        }
                        map1.put("isHaveChildren", 1);
                        map1.put("children", map1Children);
                    } else {
                        map1.put("isHaveChildren", 0);
                        map1.put("children", "");
                    }
                    mapList.add(map1);
                }
            }
        }
        // System.out.println("mapList:"+mapList.toString());
        model.addAttribute("selChapterList", mapList);

        return "cultivateWeb/course/courseInfo";
    }

    @Log("课程章节资源详情")
    @GetMapping("/couInfo/trainingResourcesDetails")
    String trainingResourcesDetails(Model model, String id, String resourceId, Integer rindex) {

        ChapterDO chapterDO = chapterService.selectById(Long.parseLong(id));
        TrainingResourcesDO resourcesDO = trainingResourcesService.selectById(Long.parseLong(resourceId));
        CoursesAndTeacherVo coursesAndTeacherVo = coursesService.selCourseDetail(chapterDO.getCoursesId());
        List<TrainingResourcesDO> trainingResourcesDOS = trainingResourcesService.findByKv("courses_id", chapterDO.getCoursesId(), "chapter_id", chapterDO.getId());
        model.addAttribute("rindexCurrent", rindex);
        if (trainingResourcesDOS != null && trainingResourcesDOS.size() > 0) {
            int i = 0;
            for (TrainingResourcesDO r : trainingResourcesDOS) {
                r.setRindex(i);
                i++;
            }
        }
        model.addAttribute("rTypeCurrent", trainingResourcesDOS.get(rindex).getType());
        model.addAttribute("rsize", trainingResourcesDOS.size());
        model.addAttribute("resources", trainingResourcesDOS);
        model.addAttribute("cou", coursesAndTeacherVo);
        model.addAttribute("resourcesDO", resourcesDO);
        model.addAttribute("chapterInfo", chapterDO);

        return "cultivateWeb/course/trainingResourcesDetails";
    }

    @Log("课程搜索")
    @GetMapping("/courseSearch")
    String courseSearch(Model model, String name) {
        model.addAttribute("name", name);
        return "cultivateWeb/course/courseSearch";
    }

    @Log("课程订单确认页面")
    @GetMapping("/courseSurePay")
    String courseSurePay(Model model, String orderId, String courseId, String type) {
        model.addAttribute("orderId", orderId);
        model.addAttribute("type", type);
        model.addAttribute("courseId", courseId);
        return "cultivateWeb/course/courseSurePay";
    }

    @Log("课程支付页面")
    @GetMapping("/coursePay")
    String coursePay(Model model, String courseOrderId, String courseAmount, String courseId) {
        model.addAttribute("courseOrderId", courseOrderId);
        model.addAttribute("courseAmount", courseAmount);
        model.addAttribute("courseId", courseId);
        return "cultivateWeb/course/coursePay";
    }

    @Log("课程支付成功页面")
    @GetMapping("/coursePaySuccess")
    String coursePaySuccess(Model model, String courseOrderId, String courseId) {
        model.addAttribute("courseOrderId", courseOrderId);
        model.addAttribute("courseId", courseId);
        return "cultivateWeb/course/coursePaySuccess";
    }


    @Log("课程计划详情")
    @GetMapping("/coursePlan")
    String coursePlan(Model model,String id, String classId){
        model.addAttribute("courseId", id);
        model.addAttribute("classId", classId);
        return "cultivateWeb/course/coursePlan";
    }

}
